﻿public class Solution {
    public int[][] ReconstructQueue(int[][] people)
    {
        // 先依据身高排序
        Array.Sort(people, (int[] x, int[] y) =>
        {
            if (x[0].Equals(y[0])) return x[1].CompareTo(y[1]);
            return y[0].CompareTo(x[0]);
        });

        List<int[]> listPeople = new List<int[]>();

        for (int i = 0; i < people.Length; i++)
        {
            listPeople.Insert(people[i][1], people[i]);
        }
        return listPeople.ToArray();
    }

    public static void Main(string[] args)
    {
        int[][] people = [[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]];
        Solution solution = new Solution();
        int[][] result = solution.ReconstructQueue(people);
        foreach (int[] res in result)
        {
            foreach (int r in res)
            {
                Console.Write($"{r} ");
            }
            Console.WriteLine();
        }
    }
}